extends "res://BaseEnemy.gd"

# 霜巨人Boss特有属性
@export var phase = 1  # 当前阶段
@export var phase_health_threshold = 0.5  # 进入第二阶段的生命值阈值
@export var ice_stomp_cooldown = 6.0  # 冰霜践踏冷却时间
@export var ice_stomp_damage = 25  # 冰霜践踏伤害
@export var ice_stomp_radius = 150.0  # 冰霜践踏半径
@export var blizzard_cooldown = 15.0  # 暴风雪冷却时间
@export var blizzard_duration = 8.0  # 暴风雪持续时间
@export var blizzard_damage = 5  # 暴风雪每秒伤害
@export var ice_boulder_cooldown = 10.0  # 冰岩投掷冷却时间
@export var ice_boulder_damage = 35  # 冰岩伤害
@export var ice_wall_cooldown = 20.0  # 冰墙冷却时间

var ice_stomp_timer = 0.0
var blizzard_timer = 0.0
var ice_boulder_timer = 0.0
var ice_wall_timer = 0.0
var can_ice_stomp = true
var can_blizzard = true
var can_ice_boulder = true
var can_ice_wall = true
var is_blizzard_active = false
var blizzard_active_timer = 0.0

func _ready():
	# 调用父类的_ready方法
	super._ready()
	
	# 设置霜巨人Boss特有属性
	max_health = 800
	current_health = max_health
	movement_speed = 50.0
	damage = 30
	experience_value = 150
	is_boss = true
	
	# 更新生命值条
	if health_bar:
		health_bar.max_value = max_health
		health_bar.value = current_health
		health_bar.visible = true  # Boss始终显示生命值条
	
	# 设置外观
	if sprite:
		sprite.modulate = Color(0.7, 0.9, 1.2)  # Boss颜色

func _physics_process(delta):
	# 调用父类的_physics_process方法
	super._physics_process(delta)
	
	# 检查是否需要进入第二阶段
	if phase == 1 and current_health <= max_health * phase_health_threshold:
		enter_phase_two()
	
	# 处理冰霜践踏冷却
	if not can_ice_stomp:
		ice_stomp_timer += delta
		if ice_stomp_timer >= ice_stomp_cooldown:
			can_ice_stomp = true
			ice_stomp_timer = 0.0
	
	# 处理暴风雪冷却
	if not can_blizzard:
		blizzard_timer += delta
		if blizzard_timer >= blizzard_cooldown:
			can_blizzard = true
			blizzard_timer = 0.0
	
	# 处理冰岩投掷冷却
	if not can_ice_boulder:
		ice_boulder_timer += delta
		if ice_boulder_timer >= ice_boulder_cooldown:
			can_ice_boulder = true
			ice_boulder_timer = 0.0
	
	# 处理冰墙冷却
	if not can_ice_wall:
		ice_wall_timer += delta
		if ice_wall_timer >= ice_wall_cooldown:
			can_ice_wall = true
			ice_wall_timer = 0.0
	
	# 处理暴风雪效果
	if is_blizzard_active:
		blizzard_active_timer += delta
		
		# 每秒对玩家造成伤害
		if target_player and is_instance_valid(target_player) and int(blizzard_active_timer) > int(blizzard_active_timer - delta):
			target_player.take_damage(blizzard_damage)
			
			# 减速效果
			if target_player.has_method("apply_slow_effect"):
				target_player.apply_slow_effect(0.7, 1.5)
		
		# 暴风雪结束
		if blizzard_active_timer >= blizzard_duration:
			is_blizzard_active = false
	
	# 根据阶段和冷却状态使用技能
	if phase == 2:
		# 第二阶段更积极地使用技能
		if can_ice_stomp and target_player and is_instance_valid(target_player):
			var distance = global_position.distance_to(target_player.global_position)
			if distance < 120 and randf() < 0.04:  # 每帧4%几率
				ice_stomp_attack()
		
		if can_blizzard and randf() < 0.01:  # 每帧1%几率
			activate_blizzard()
		
		if can_ice_boulder and target_player and is_instance_valid(target_player):
			var distance = global_position.distance_to(target_player.global_position)
			if distance < 300 and distance > 100 and randf() < 0.03:  # 每帧3%几率
				throw_ice_boulder()
		
		if can_ice_wall and target_player and is_instance_valid(target_player):
			var distance = global_position.distance_to(target_player.global_position)
			if distance < 200 and randf() < 0.02:  # 每帧2%几率
				create_ice_walls()
	else:
		# 第一阶段较少使用技能
		if can_ice_stomp and target_player and is_instance_valid(target_player):
			var distance = global_position.distance_to(target_player.global_position)
			if distance < 120 and randf() < 0.02:  # 每帧2%几率
				ice_stomp_attack()
		
		if can_blizzard and randf() < 0.005:  # 每帧0.5%几率
			activate_blizzard()
		
		if can_ice_boulder and target_player and is_instance_valid(target_player):
			var distance = global_position.distance_to(target_player.global_position)
			if distance < 300 and distance > 100 and randf() < 0.02:  # 每帧2%几率
				throw_ice_boulder()
		
		if can_ice_wall and target_player and is_instance_valid(target_player):
			var distance = global_position.distance_to(target_player.global_position)
			if distance < 200 and randf() < 0.01:  # 每帧1%几率
				create_ice_walls()

func enter_phase_two():
	phase = 2
	
	if animation_player and animation_player.has_animation("transform"):
		animation_player.play("transform")
	
	# 增强属性
	movement_speed *= 1.2
	damage *= 1.5
	
	# 视觉效果
	if sprite:
		sprite.modulate = Color(0.5, 0.7, 1.5)  # 更亮的蓝色
		sprite.scale *= 1.2  # 更大的尺寸
	
	# 立即使用暴风雪
	activate_blizzard()
	
	print("霜巨人进入第二阶段!")

func ice_stomp_attack():
	if animation_player and animation_player.has_animation("stomp"):
		animation_player.play("stomp")
	
	# 等待动画中点
	await get_tree().create_timer(0.5).timeout
	
	# 创建冰霜践踏效果
	var stomp = preload("res://IceStompEffect.tscn").instantiate()
	get_tree().get_root().add_child(stomp)
	stomp.global_position = global_position
	stomp.damage = ice_stomp_damage
	stomp.radius = ice_stomp_radius
	stomp.source = self
	
	# 检测范围内的玩家
	if target_player and is_instance_valid(target_player):
		var distance = global_position.distance_to(target_player.global_position)
		if distance <= ice_stomp_radius:
			# 对玩家造成伤害
			target_player.take_damage(ice_stomp_damage)
			
			# 击退效果
			var knockback_direction = (target_player.global_position - global_position).normalized()
			if target_player.has_method("apply_knockback"):
				target_player.apply_knockback(knockback_direction * 250)
			
			# 减速效果
			if target_player.has_method("apply_slow_effect"):
				target_player.apply_slow_effect(0.5, 3.0)
	
	# 重置冰霜践踏冷却
	can_ice_stomp = false
	ice_stomp_timer = 0.0
	
	print("霜巨人使用冰霜践踏!")

func activate_blizzard():
	if animation_player and animation_player.has_animation("blizzard"):
		animation_player.play("blizzard")
	
	# 设置暴风雪状态
	is_blizzard_active = true
	blizzard_active_timer = 0.0
	can_blizzard = false
	blizzard_timer = 0.0
	
	# 创建暴风雪效果
	var blizzard = preload("res://BlizzardEffect.tscn").instantiate()
	get_tree().get_root().add_child(blizzard)
	blizzard.global_position = global_position
	blizzard.duration = blizzard_duration
	blizzard.damage_per_second = blizzard_damage
	
	print("霜巨人释放暴风雪!")

func throw_ice_boulder():
	if animation_player and animation_player.has_animation("throw"):
		animation_player.play("throw")
	
	# 创建冰岩投射物
	var boulder = preload("res://IceBoulderProjectile.tscn").instantiate()
	get_tree().get_root().add_child(boulder)
	
	# 设置冰岩属性
	var direction = Vector2.RIGHT
	if target_player and is_instance_valid(target_player):
		direction = (target_player.global_position - global_position).normalized()
	
	boulder.global_position = global_position
	boulder.direction = direction
	boulder.damage = ice_boulder_damage
	boulder.source = self
	
	# 重置冰岩投掷冷却
	can_ice_boulder = false
	ice_boulder_timer = 0.0
	
	print("霜巨人投掷冰岩!")

func create_ice_walls():
	if animation_player and animation_player.has_animation("ice_wall"):
		animation_player.play("ice_wall")
	
	# 创建多个冰墙围绕玩家
	if target_player and is_instance_valid(target_player):
		var wall_count = 4 if phase == 1 else 6
		
		for i in range(wall_count):
			var angle = TAU * i / wall_count
			var offset = Vector2(cos(angle), sin(angle)) * 80
			var wall_position = target_player.global_position + offset
			
			var ice_wall = preload("res://IceWall.tscn").instantiate()
			get_tree().get_root().add_child(ice_wall)
			ice_wall.global_position = wall_position
			ice_wall.duration = 5.0
			ice_wall.source = self
	
	# 重置冰墙冷却
	can_ice_wall = false
	ice_wall_timer = 0.0
	
	print("霜巨人创建冰墙!")

# 重写攻击方法
func attack_player():
	if phase == 2 and randf() < 0.4:  # 第二阶段40%几率使用冰霜践踏
		ice_stomp_attack()
	else:
		# 普通攻击
		super.attack_player()

# 重写受伤方法，添加阶段转换逻辑
func take_damage(amount):
	super.take_damage(amount)
	
	# 检查是否需要进入第二阶段
	if phase == 1 and current_health <= max_health * phase_health_threshold:
		enter_phase_two()
